var amapPlugin;
import {setLocation,wxLogin,updateInvite,banner,lotteryCount,isFollow} from '@/api/api.js'
import {mapGetters} from 'vuex'
import amap from '@/common/amap-wx.js';
var log = require('@/util/log.js');
export default{
	data() {
		return {
			typeArr:['全日房','秒计房'],
			search:"",
			show:false,
			mode:'range',
			dayNum:1,
			minDate: this.$u.timeFormat(new Date().getTime()-1000*60*60*6),
			modalName:null,
			modalName1:null,
			modalName2:null,
			modalName3:null,
			bannerList:[],
		};
	},
	onLoad(e) {
		const Authorization = uni.getStorageSync('Authorization');
		this.initTime();
		if(Authorization){
			this.getLotteryCount();
			this.getLocation();
		}
		let _this = this;
		uni.$once('loginSuccess',({openId})=>{
			if(e.scene){
				const scene = decodeURIComponent(e.scene);
				updateInvite({invite:scene,openId}).then(res=>{
					if(res.code==200&&res.data){
						this.modalName = 'Image';
					}
				})
			}
		})
	},
	onShow(){
		if(uni.userInfo&&!uni.userInfo.gps){
			uni.authorize({
				scope:"scope.userLocation",
				success:()=>{
					this.getLocation();
				},
				fail:err=>{
					console.log(err);
				}
			})
		}
		if(this.bannerList.length<=0){
			this.getBanner();
		}
		
	},
	computed:{
		...mapGetters(['type','time','chooseCity'])
	},
	methods:{
		officialAccountLoad(e){
			log.info(JSON.stringify(e));
			if(e.detail.status==0){
				setTimeout(()=>{
					isFollow({openId:uni.openId}).then(res=>{
						if(res.code==200&&!res.data){
							this.modalName = 'guanzhu'
						}
					})
				},1000)
			}
		},
		officialAccountError(e){
			log.error(JSON.stringify(e));
		},
		goArea(){
			uni.navigateTo({
				url:"/pages/index/cityList/cityList"
			})
		},
		imageHide(str){
			this[str]='hide';
			setTimeout(()=>{
				this.modalName = null;
			},300)
		},
		goView(item){
			let urlReg = /^https:\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$/;
			if(urlReg.test(item.url)){
				uni.navigateTo({
					url:"/pages/index/new/new?src="+item.url
				})
			}
		},
		goLotteryCount(){
			uni.navigateTo({
				url:"/pages/index/luckDraw/luckDraw"
			});
			this.modalName = null;
			this.modalName2 = 'hide';
		},
		getBanner(){
			banner().then(res=>{
				if(res.code==200){
					this.bannerList = res.data;
				}
			})
		},
		getLotteryCount(){
			lotteryCount().then(res=>{
				if(res.code==200&&res.data>0){
					this.modalName = 'lotteryCount';
				}
			})
		},
		searchHotel(){
			uni.navigateTo({
				url:"../../index/hotelList/hotelList?search="+this.search,
			})
		},
		tabSelect(e){
			this.$store.dispatch('SetType',e.currentTarget.dataset.id)
			this.initTime();
		},
		changeTime(e){
			let time = [];
			if(this.type){
				time[0]=e.result;
			}else{
				if(e.startDate==e.endDate){
					return;
				}
				time[0]=e.startDate;
				time[1]=e.endDate;
				this.dayNum = parseInt(((new Date(e.endDate).getTime() - new Date(e.startDate).getTime())/(1000*60*60*24)));
			}
			this.$store.dispatch('SetTime',time);
		},
		initTime(){
			let time=[];
			time.push(this.$u.timeFormat(new Date().getTime()-1000*60*60*6));
			let nextDay = new Date(time[0]).getTime()+(1000*60*60*24);
			time.push(this.$u.timeFormat(nextDay));
			this.$store.dispatch("SetTime",time);
			this.dayNum=1;
		},
		getLocation(){
			amapPlugin = new amap.AMapWX({
				key: this.gdKey
			});
			uni.login({
				success: (e) => {
					wxLogin(e).then(aaa=>{
						amapPlugin.getRegeo({
							success: (res) => {
								log.info(JSON.stringify(res));
								let chooseCity = {
									areaName:res[0].regeocodeData.addressComponent.city,
									areaCode:String(res[0].regeocodeData.addressComponent.adcode).substr(0,4)
								};
								setLocation({
									latitude:res[0].latitude,
									longitude:res[0].longitude,
									adcode:res[0].regeocodeData.addressComponent.adcode,
									openid:aaa.data.openid
								});
								this.$store.dispatch('SetArea',chooseCity)
							},
							fail: (res) => {
								log.error(JSON.stringify(res));
								console.log('定位失败：',res);
							}
						});
					})
				}
			})
		}
	}
}